home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 6187 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.3 KB  |  105 lines

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: I can't print the date in the format I want.
  5. Date: Fri, 23 Feb 96 00:35:32 GMT
  6. Organization: none
  7. Message-ID: <825035732snz@genesis.demon.co.uk>
  8. References: <4g5nbf$8s0@newsbf02.news.aol.com> <3129e355.114134@news.iquest.net>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <3129e355.114134@news.iquest.net>
  15.            rclark@iquest.net "Robert B. Clark" writes:
  16.  
  17. >On 17 Feb 1996 18:11:43 -0500, asciizero@aol.com (ASCII zero) wrote:
  18. >
  19. >>I am trying to use strftime() to format the date into YY-MM-DD. The format
  20. >>seems to be working but I am unable to actually print the formatted
  21. >>string. It compiles just fine using gcc. What have I overlooked?
  22. >
  23. >You need to check your compiler and make sure that you've not disabled
  24. >those oh-so-useful warning messages:
  25. >
  26. >>main()
  27. >
  28. >NC.
  29. >
  30. >On second thought, I'll comment anyway.  Your function ought to return
  31. >some sort of value, if for no other reason than to reduce the flamewars
  32. >people tend to get into over various permutations of the main()
  33. >declaration:
  34. >
  35. >        int main(void)
  36.  
  37. Good advice but be aware that if the return type of a function
  38. is unspecified it defaults to int. so ``main() {}'' defines a function that
  39. takes no arguments and returns int.
  40.  
  41. >> debug = strftime(str, 50, "The current date is %y-%m-%d", ptr);
  42. >
  43. >"Possible use of 'str' before definition."
  44.  
  45. Definite misuse of terminology :-)
  46.  
  47. str was defined on a previous line by char *str; . However this code certainly
  48. reads the value of str before setting it i.e. it passes an undefined value
  49. to strftime().
  50.  
  51. ...
  52.  
  53. >> getchar();                           /* OK up to this point? */
  54. >
  55. >"Code has no effect."
  56. >
  57. >Doesn't do anything but wait for the user to press a key.  If that was
  58. >your intent, it would be better to express this as
  59. >
  60. >        if getchar();
  61.  
  62. Perhaps you meant:
  63.  
  64.           if (getchar());
  65.  
  66. However all the if statement seems to do is obfuscate the code.
  67.  
  68. ...
  69.  
  70. >Here is a slight revision of your code, with the disastrous null pointer
  71. >taken care of by explicitly declaring a char array for str instead of
  72. >just a char pointer:
  73. >
  74. >#include <stdio.h>
  75. >#include <time.h>
  76. >#include <string.h>             /* For strlen() function */
  77. >#define SLEN            50      /* Allocate 50 chars for str */
  78. >
  79. >int main(void)  
  80. >{
  81. >   struct tm *ptr;
  82. >   time_t lt;
  83. >   char str[SLEN];      /* Explicitly allocate 50 chars for str */
  84. >   int debug;
  85. >
  86. >   lt = time(NULL);
  87. >   ptr = localtime(<);
  88. >   printf("\nThe system clock shows: %s\n\n",asctime(ptr));
  89. >   debug=strftime(str,SLEN,"The current date is %y-%m-%d",ptr);
  90. >   printf("\nThe value of 'debug' is %d"
  91. >              "\nThe length of 'str' is %d", debug,strlen(str));
  92. >   printf("\nFormatted time string: \"%s\"",str);
  93. >   return debug;
  94. >}
  95.  
  96. The only portable values to return from main() are 0, EXIT_SUCCESS and
  97. EXIT_FAILURE (the last 2 being defined in stdlib.h). return 0 or return
  98. EXIT_SUCCESS is probably sensible here.
  99.  
  100. -- 
  101. -----------------------------------------
  102. Lawrence Kirby | fred@genesis.demon.co.uk
  103. Wilts, England | 70734.126@compuserve.com
  104. -----------------------------------------
  105.